package com.end;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.function.Predicate;

public class Exam1 {

    public static void main(String[] args) throws Exception {
        List<Order> list = new ArrayList<>();
        SAXReader saxReader = new SAXReader();
        Document document = saxReader.read("./src/com/end/orders.xml");
        for(Element element : document.getRootElement().elements()){
            long order_id = Long.parseLong(element.attributeValue("id"));
            String name = element.elementText("name");
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            LocalDateTime time = LocalDateTime.parse(element.elementText("time"), formatter);
            double price = Double.parseDouble(element.elementText("double"));
            Order order = new Order(order_id, name, time, price);
            list.add(order);
        }
        //1
        list.stream().filter((order) -> order.getTime().isBefore(LocalDateTime.now())).forEach(System.out::println);
        //2
        Optional<Order> order = list.stream().max(Comparator.comparingDouble(Order::getPrice));
        System.out.println(order.get());
        list.stream().sorted((o1, o2) -> Double.compare(o2.getPrice(), o1.getPrice())).forEach(System.out::println);
    }
}
